home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9589 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  72 lines

  1. Path: lade.news.pipex.net!pipex!dircon!usenet
  2. From: sridgway@dircon.co.uk (Steven Ridgway)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: ?Help - struct array of strings
  5. Date: Mon, 11 Mar 1996 22:11:36 GMT
  6. Organization: Direct Connection
  7. Message-ID: <4i28ln$2i0@newsgate.dircon.co.uk>
  8. References: <4hvpgp$gu7@ftp.netgate.net>
  9. NNTP-Posting-Host: gw4-129.pool.dircon.co.uk
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. Tamara Johnson <malihini@netgate.net> wrote:
  13.  
  14. >/*
  15. >   program to enter author(s), title(s)
  16. >   then display entries.
  17. >   why is printf displaying strange things?
  18. >   (see output at end)
  19. >*/
  20.  
  21. >#include <stdlib.h>
  22. >#include <stdio.h>
  23. >#include <conio.h>
  24. >#include <string.h>
  25.  
  26. >#define MAX 5
  27.  
  28. >struct catalog{
  29. >  char name[80];        /* author name */
  30. >  char title[80];    /* title */ 
  31. >}cat[80];
  32.  
  33. >void main()
  34.  
  35. >{
  36. >int i;
  37.  
  38. >for(i=0;i<MAX;i++) 
  39. >  {
  40. >  printf("Enter author name (ENTER to quit); ");
  41. >  gets(cat[i].name);
  42. >  if(!*cat[i].name) break;
  43. >  printf("Enter title: ");
  44. >  gets(cat[i].title);
  45. >  }
  46. >for(i=0;i<MAX;i++)
  47. >  printf("%c %c\n", cat[i].name, cat[i].title);
  48. >}
  49. >/*
  50. >output;
  51.  
  52. >Enter author name (ENTER to quit); tom
  53. >Enter title: pickled peppers
  54. >Enter author name (ENTER to quit); sam
  55. >Enter title: hop on pop
  56. >Enter author name (ENTER to quit);
  57. >` t
  58. >Ω ú
  59. >_ -
  60. >+ _
  61. >  ╢
  62. >*/
  63.  
  64. Hi,
  65.  
  66. In your printf try using %s rather than %c. 
  67. %c means print a char, but you are passing a string (ie a pointer), so
  68. you see the first byte of your pointer.
  69.  
  70. steven ridgway (sridgway@dircon.co.uk)
  71.  
  72.